package cn.nie.service.impl;

import cn.nie.entity.Document;
import cn.nie.hbase.HbaseUtils;
import cn.nie.service.IndexService;
import org.apache.commons.io.FileUtils;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.Doc;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Service("indexService")
public class IndexServiceImpl implements IndexService {

    @Autowired
    private SolrServer solrServer;

    public void flashIndex() throws IOException {
        List<Document> arrayList = new ArrayList<Document>();
        File file = new File("D:\\2.txt");
        List<String> list = FileUtils.readLines(file,"UTF8");
        for(String line : list){
            Document doc = new Document();
            String[] split = line.split("\t");
            System.out.print(split[0]);
            int parseInt = Integer.parseInt(split[0].trim());
            doc.setId(parseInt);
            doc.setTitle(split[1]);
            doc.setAuthor(split[2]);
            doc.setDescribe(split[3]);
            doc.setContent(split[3]);
            arrayList.add(doc);
            SolrInputDocument document = new SolrInputDocument();
            document.setField("id",split[0]);
            document.setField("item_title",split[1]);
            document.setField("item_author",split[2]);
            document.setField("item_describe",split[3]);
            try {
                solrServer.add(document);
                solrServer.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        HbaseUtils hbaseUtils = new HbaseUtils();
        for (Document doc : arrayList) {
            hbaseUtils.put(hbaseUtils.TABLE_NAME, doc.getId()+"", hbaseUtils.COLUMNFAMILY_1, hbaseUtils.COLUMNFAMILY_1_TITLE, doc.getTitle());
            hbaseUtils.put(hbaseUtils.TABLE_NAME, doc.getId()+"", hbaseUtils.COLUMNFAMILY_1, hbaseUtils.COLUMNFAMILY_1_AUTHOR, doc.getAuthor());
            hbaseUtils.put(hbaseUtils.TABLE_NAME, doc.getId()+"", hbaseUtils.COLUMNFAMILY_1, hbaseUtils.COLUMNFAMILY_1_DESCRIBE, doc.getDescribe());
            hbaseUtils.put(hbaseUtils.TABLE_NAME, doc.getId()+"", hbaseUtils.COLUMNFAMILY_1, hbaseUtils.COLUMNFAMILY_1_CONTENT, doc.getContent());
        }

    }
    public void createIndex(){

    }
}

